//
//  LovingHeartVC.m
//  智慧小区
//
//  Created by 高永杨 on 2017/3/17.
//  Copyright © 2017年 zhongchuang. All rights reserved.
//

#import "LovingHeartVC.h"
#import "HelpMessageVC.h"
#import "DonationsVC.h"
@interface LovingHeartVC ()<UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *layoutCenter;
/**滚动的view*/
@property (nonatomic,strong) UIScrollView *contentView;
/**选中的butt*/
@property(nonatomic,strong)UIButton *selectButt;
@property (weak, nonatomic) IBOutlet UIView *indorV;
@property (weak, nonatomic) IBOutlet UIView *titleV;

@end

@implementation LovingHeartVC
-(void)goBack
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNav:self];
    self.tabBarController.tabBar.hidden = YES;
   
   UIButton *buttTap1 = [self.view viewWithTag:1001];
    UIButton *buttTap2 = [self.view viewWithTag:1002];
    switch (self.type) {
        case citizen:
            break;
        case HelpOther:
            [buttTap1 setTitle:@"资源共享" forState:UIControlStateNormal];
           self.navigationItem.title = @"我的互助";
            [buttTap2 setTitle:@"互助记录" forState:UIControlStateNormal];
            break;
        default:
            break;
    }
    [self setupController];
    [self scrollContentView];
    // Do any additional setup after loading the view from its nib.
}
-(void)scrollContentView
{
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    UIScrollView *scroview = [[UIScrollView alloc]init];
    scroview.frame = CGRectMake(0, 44, GyyScreenW, GyyScreenH-108);
    GYYLog(@"scroview--%@",scroview);
    scroview.delegate = self;
    self.contentView = scroview;
    [self.view insertSubview:scroview atIndex:0];
    CGFloat conSizeWidth = self.childViewControllers.count * [UIScreen mainScreen].bounds.size.width;
    scroview.pagingEnabled = YES;
    scroview.showsHorizontalScrollIndicator = NO;
    scroview.showsHorizontalScrollIndicator = NO;
    scroview.contentSize = CGSizeMake(conSizeWidth, 100);
    [self scrollViewDidEndScrollingAnimation:scroview];
}
-(void)receive:(HelpMessageVC *)baseContro
{
    switch (self.type) {
        case HelpOther:
         baseContro.myType = @"HelpOther";
            break;
        case citizen:
            baseContro.myType = @"citizen";
            break;
        default:
            break;
    }
}

-(void)setupController
{
    HelpMessageVC *vieWords1 =  [[HelpMessageVC alloc]init];
    vieWords1.type = Help;
    [self receive:vieWords1];
    vieWords1.view.bounds = [UIScreen mainScreen].bounds;
    vieWords1.view.backgroundColor = GYYrgbColor(50, 20, 20);
    [self addChildViewController:vieWords1];
    HelpMessageVC *vieWords2 =  [[HelpMessageVC alloc]init];
    vieWords2.type = Love;
    [self receive:vieWords2];
    vieWords2.view.bounds = [UIScreen mainScreen].bounds;
    vieWords2.view.backgroundColor = [UIColor cyanColor];
    [self addChildViewController:vieWords2];
    if (self.type == Help) {
        HelpMessageVC *vieWords3 =  [[HelpMessageVC alloc]init];
        vieWords3.type = Resource;
        [self receive:vieWords3];
        vieWords3.view.bounds = [UIScreen mainScreen].bounds;
        vieWords3.view.backgroundColor = [UIColor redColor];
        [self addChildViewController:vieWords3];
    }else{
    DonationsVC *vieWords4 =  [[DonationsVC alloc]init];
    vieWords4.view.bounds = [UIScreen mainScreen].bounds;
    vieWords4.view.backgroundColor = GYYrgbColor(10, 20, 50);
    [self addChildViewController:vieWords4];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(IBAction)tabClick:(UIButton *)sender {
    self.selectButt.enabled = YES;
    sender.enabled = NO;
    self.selectButt = sender;
    [UIView animateWithDuration:0.25 animations:^{
//        [self.view layoutIfNeeded];
        self.indorV.centerX = sender.centerX;
    }];
    CGPoint offset = self.contentView.contentOffset;
    offset.x = (sender.tag - 1000)*self.contentView.width;
    [self.contentView setContentOffset:offset animated:YES];
}


-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [self scrollViewDidEndScrollingAnimation:scrollView];
    NSInteger index = scrollView.contentOffset.x/scrollView.width+1000;
    [self tabClick:[self.titleV viewWithTag:index]];
}

#pragma mark -- <UIScrollViewDelegate>
-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    //    NSString *url = nil;
    NSInteger index = scrollView.contentOffset.x/scrollView.width;
   UIViewController *vc = self.childViewControllers[index];
    GYYLog(@"vc.view--%@",vc.view);
    vc.view.x = scrollView.contentOffset.x;
    vc.view.height = scrollView.height;
    vc.view.y = 0;
    [scrollView addSubview:vc.view];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
